package dnl.jexem.cmd;

import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;

/**
 * Implements the simplest <code>CommandExcecution</code> that executes on the local machine. Blocking only
 * with no timeout.
 *
 * @author Daniel Orr
 *
 */
public class SimpleLocalCommandExcecution extends BaseLocalCommandExecution {

    protected String output;
    protected String errorOutput = "";

    public SimpleLocalCommandExcecution(String... command) {
        super(command);
    }

    public SimpleLocalCommandExcecution(Command command) {
        super(command);
    }

    @Override
    public String getOutput() {
        return output;
    }

    @Override
    public String getErrorOutput() {
        return errorOutput;
    }

    
    @Override
    public void executeCommand(long timeout, boolean block) {
        if (timeout > 0) {
            throw new UnsupportedOperationException("Timeout is not supported. Use the non 'Simple' version");
        }
        super.executeCommand(timeout, block);
    }
    
    @Override
    protected void handleProcessIO(final InputStream inputStream, final InputStream errorStream, boolean block) throws IOException {
        if (!block) {
            throw new UnsupportedOperationException("Non blocking is not supported. Use the non 'Simple' version");
        }
        output = IOUtils.toString(inputStream);
        errorOutput = IOUtils.toString(errorStream);
    }

}
